package com.zyd.controller.station;

import com.zyd.constants.ErrorDef;
import com.zyd.context.HeaderContext;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.StationEntity;
import com.zyd.io.vo.res.ResBody;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.page.PageBean;
import com.zyd.service.station.StationService;
import com.zyd.service.log.LogService;

import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stations")
public class StationController {

    @Autowired
    private StationService service;

    @Autowired
    private LogService logservice;

    @Autowired
    private ServletContext context;


    @PostMapping(value = "/addStation", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addStation(@RequestBody StationEntity station) {
        try {
            //记录操作日志
            logservice.addLog("", "增加基站");
            service.addStation(station);
            return new ResBody(ErrorDef.SUCCESS, "新增基站成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "新增基站失败");
        }
    }

    @PostMapping(value = "/updateStation", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateStation(@RequestBody StationEntity station) {
        try {
            //记录操作日志
            logservice.addLog("", "修改基站");
            service.updateStation(station);
            return new ResBody(ErrorDef.SUCCESS, "修改基站成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改基站失败");
        }
    }

    @PostMapping(value = "/deleteStation", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteStation(@RequestBody Map<String, Object> map) {
        try {
            //记录操作日志
            logservice.addLog("", "删除基站");
            service.deleteStation((String) map.get("id"));
            return new ResBody(ErrorDef.SUCCESS, "删除基站成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除基站失败");
        }
    }

    @PostMapping(value = "/stationList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findUseList(@RequestBody MultiQueryPageVO stationRequestDTO) {
        try {
            //记录操作日志
            logservice.addLog("", "查询基站列表");
            PageBean<StationEntity> page = service.findStationPage(stationRequestDTO);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询基站列表成功");
            resBody.setData(page);
            /**
             * 保存查询的条件 导出使用
             */
            context.setAttribute("stationExcelParam"+ HeaderContext.get().get("userid"), stationRequestDTO);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询基站列表失败");
        }
    }


    @PostMapping(value = "/findStationCount", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findStationCount(@RequestBody Map<String, Object> request) {
        try {
            //记录操作日志
            logservice.addLog("", "查询基站统计");
            List<Map<String, Object>> list = service.findStationCount(request);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询基站统计成功");
            resBody.setData(list);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询基站统计失败");
        }
    }

    @PostMapping(value = "/findStation", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findUseList(@RequestBody Map<String, Object> map) {
        try {
            //记录操作日志
            logservice.addLog("", "查询基站明细");
            StationEntity ret = service.findStation((String) map.get("id"));

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询基站成功");
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询基站失败");
        }
    }


    @GetMapping(value = "excelExport/{userid}")
    public void downloadAllClassmate(@PathVariable String userid, HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        //记录操作日志
        logservice.addLog(userid, "导出基站");
        String fileName = "zyd_data" + ".xls";//设置要导出的文件的名字

        /**
         * 取出session中的条件
         */
        MultiQueryPageVO stationExcelParam = (MultiQueryPageVO) context.getAttribute("stationExcelParam"+userid);
        stationExcelParam.setPageSize(100000000);
        stationExcelParam.setCurrentPage(1);
        List<StationEntity> stationList = service.findStationList(stationExcelParam,userid);

        int rowNum = 1;
        //新增数据行，并且设置单元格数据
        String[] headers = {"基站编号", "基站名称", "基站地址", "基站辖区", "经度", "纬度", "基站状态", "域名指向端口"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (StationEntity entity : stationList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(entity.getStationcode());
            row1.createCell(1).setCellValue(entity.getStationname());
            row1.createCell(2).setCellValue(entity.getAddress());
            row1.createCell(3).setCellValue(entity.getArea());
            row1.createCell(4).setCellValue(entity.getJd());
            row1.createCell(5).setCellValue(entity.getWd());
            row1.createCell(6).setCellValue(entity.getStationstatus());
            row1.createCell(7).setCellValue(entity.getPort());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
    @PostMapping(value = "/findStationAll",consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findStationAll(@RequestBody Map<String, Object> request) {
        try {
            //记录操作日志
            logservice.addLog("","查询基站统计");
            List<StationEntity> list = service.findStationAll(request);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询基站统计成功");
            resBody.setData(list);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询基站统计失败");
        }
    }
}
